﻿using pcBLL;
using pcCore;
using pcObjectLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PunchClock.UI.Web.Controllers
{
    [Authorize]
    public class ManageController : BaseController
    {
        //
        // GET: /Manage/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Logs()
        {
            if (UserCore.AdminUsers().Any(y => operatingUser.userTypeId.Equals(y)))
            {
                return View();
            }
            else
                return Content("aaaha, please do not be smart", "text");
           
        }

        [Authorize]
        [HttpPost]
        public JsonResult Approve( FormCollection col)
        {
            PunchObjectLibrary obj = new PunchObjectLibrary();

            int pId;
            int.TryParse(col.Get("models[0][punchId]"), out pId);
            obj.punchId = pId;

            int uId;
            int.TryParse(col.Get("models[0][userId]"), out uId);
            obj.userId = uId;

            obj.comments = col.Get("models[0][comments]");

            bool approved;
            bool.TryParse(col.Get("models[0][isManagerAccepted]"),out approved);
            obj.isManagerAccepted = approved;

            UserBLL ub = new UserBLL();
            string userTimeZoneId = ub.getTimeZoneOfUser(uId);
            //var ci = new System.Globalization.CultureInfo("en-us");
            obj.punchIn = Parse.ToUtcTime(jsOffsetTime: col.Get("models[0][punchIn]"), TimeZoneId: userTimeZoneId);
            obj.punchOut = Parse.ToUtcTime(jsOffsetTime: col.Get("models[0][punchOut]"), TimeZoneId: userTimeZoneId); 

           obj.punchDate = Parse.ToDate(col.Get("models[0][punchDate]"));
           obj.punchDate = new DateTime(obj.punchDate.Year, obj.punchDate.Month, obj.punchDate.Day, obj.punchIn.Hours, obj.punchIn.Minutes, obj.punchIn.Seconds);
           obj.punchDate = TimeZoneInfo.ConvertTimeToUtc(obj.punchDate, TimeZoneInfo.FindSystemTimeZoneById(operatingUser.registeredTimeZone));

           bool isApproved = false;
            if (UserCore.AdminUsers().Any(y => operatingUser.userTypeId.Equals(y)))
            {
                PunchBLL pb = new PunchBLL();
                isApproved = pb.Approve(punchObj: obj, opUserId: operatingUser.userId);
            }
            //return Json(new { isSuccess = isApproved });
            return Json(true);
        }

        [Authorize]
        public JsonResult OpenLogs()
        {
            if (UserCore.AdminUsers().Any(y => operatingUser.userTypeId.Equals(y)))
            {
                List<PunchObjectLibrary> obj = new List<PunchObjectLibrary>();
                PunchBLL pb = new PunchBLL();
                obj = pb.GetOpenLogs(opUserId: operatingUser.userId);

                foreach (var punch in obj)
                {
                    var pIn = TimeZoneInfo.ConvertTimeFromUtc(punch.punchDate.Date + punch.punchIn, TimeZoneInfo.FindSystemTimeZoneById(operatingUser.registeredTimeZone));
                    var pOut = punch.punchOut.HasValue ? TimeZoneInfo.ConvertTimeFromUtc(punch.punchDate.Date + punch.punchOut.Value, TimeZoneInfo.FindSystemTimeZoneById(operatingUser.registeredTimeZone)) : DateTime.MinValue;

                    if (punch.punchOut.HasValue)
                        punch.duration = new TimeSpan(pOut.TimeOfDay.Hours, pOut.TimeOfDay.Minutes, pOut.TimeOfDay.Seconds) - new TimeSpan(pIn.TimeOfDay.Hours, pIn.TimeOfDay.Minutes, pIn.TimeOfDay.Seconds);
                    int tmpHoursInSecs;
                    int.TryParse(punch.duration.TotalSeconds.ToString(), out tmpHoursInSecs);
                    punch.Hours = tmpHoursInSecs;
                }
                return Json(obj, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(false, JsonRequestBehavior.AllowGet);
        }

    }
}
